Skip to content

Feat/checkout items#10

Open
willymwai wants to merge 12 commits intomasterfrom
feat/checkout-items
Open

Feat/checkout items#10
willymwai wants to merge 12 commits intomasterfrom
feat/checkout-items

Conversation

@willymwai
Copy link
Member

@willymwai willymwai commented Jan 28, 2025

PR Type

Enhancement, Bug fix


Description

  • Added support for shipping rates and payment methods in checkout.

    • Introduced new utilities to fetch shipping rates and payment methods.
    • Updated checkout response to include shipping rates and payment methods.
  • Refactored checkout submission to simplify action handling.

    • Replaced onSuccessAction with a unified action parameter.
    • Updated type definitions to reflect the new structure.
  • Enhanced cart normalization with additional fields.

    • Included billing and shipping addresses, payments, and shipments.
  • Fixed issues with shipping rates array concatenation.


Changes walkthrough 📝

Relevant files
Enhancement
12 files
use-checkout.tsx
Added shipping rates and payment methods to checkout handler
+16/-3   
use-submit-checkout.tsx
Refactored checkout submission with unified action handling
+19/-12 
checkout.ts
Updated checkout types with shipping and payment details 
+56/-10 
address.ts
Made address fields optional in type definitions                 
+10/-10 
registerCheckoutProvider.tsx
Updated checkout provider to support new action structure
+48/-18 
cart.ts
Extended cart types with shipping and payment details       
+65/-5   
index.ts
Added attributes for shipping rates, payments, and shipments
+87/-0   
get-cart.ts
Included billing and shipping addresses, payments, and shipments in
cart fetch
+4/-0     
get-payment-methods.ts
Added utility to fetch payment methods                                     
+37/-0   
get-shipping-rates.ts
Added utility to fetch shipping rates                                       
+67/-0   
normalize-cart.ts
Enhanced cart normalization with shipping and payment details
+114/-1 
submit-checkout.ts
Refactored checkout submission to use unified action         
+14/-25 
Dependencies
3 files
package.json
Updated package version to 0.2.1                                                 
+1/-1     
package.json
Updated `commerce-spree` dependency to version 0.2.1         
+1/-1     
package.json
Updated `commerce-spree` dependency to version 0.2.1         
+1/-1     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @qodo-code-review
    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 No relevant tests
    🔒 Security concerns

    Sensitive information exposure:
    The checkout submission process handles sensitive data like email, billing address, payment information etc. The code should ensure this data is properly encrypted during transmission and not logged or exposed in error messages.

    ⚡ Recommended focus areas for review

    Error Handling

    The error handling for checkout submission only checks for 404 status. Other error cases like validation errors, server errors etc. should be handled appropriately.

    } catch (updateItemError) {
      if (
        updateItemError instanceof FetcherError &&
        updateItemError.status === 404
    Null Check

    The function returns null if token is undefined but the return type doesn't indicate nullable return value. This could cause runtime errors.

    if (!token) {
      return null
    } else {
    Conditional Fetching

    Shipping rates and payment methods are only fetched if shipping address exists. Consider cases where these might be needed without shipping address.

    if (cart.shippingAddress) {
      shippingRates = await getShippingRates(fetch)
      paymentMethods = await getPaymentMethods(fetch)
    }

    @qodo-code-review
    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add error handling for API calls

    Add error handling for the asynchronous API calls to getShippingRates and
    getPaymentMethods to prevent potential runtime errors if the API calls fail.

    platform/canvas-packages/internal_pkgs/spree/src/checkout/use-checkout.tsx [31-34]

     if (cart.shippingAddress) {
    -  shippingRates = await getShippingRates(fetch)
    -  paymentMethods = await getPaymentMethods(fetch)
    +  try {
    +    shippingRates = await getShippingRates(fetch)
    +    paymentMethods = await getPaymentMethods(fetch)
    +  } catch (error) {
    +    console.error('Failed to fetch shipping rates or payment methods:', error)
    +    shippingRates = null
    +    paymentMethods = null
    +  }
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion adds important error handling for critical API calls that could fail silently. This improves reliability and debugging capabilities by properly handling potential errors and preventing undefined behavior.

    8
    Validate required checkout submission fields

    Add input validation for required fields before submitting the checkout to prevent
    invalid submissions and potential API errors.

    platform/canvas-packages/internal_pkgs/spree/src/checkout/use-submit-checkout.tsx [52-53]

    -if (!email) {
    +if (!email || !action) {
       throw new ValidationError({
    +    message: 'Email and action are required fields for checkout submission'
    +  })
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion improves input validation by checking for the required 'action' field in addition to 'email', which helps prevent invalid API calls and provides clearer error messages to users.

    7

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    1 participant